// 2025/9/19
// 简化路径

class Solution {
public:
    string simplifyPath(string path) {
        int n = path.size(), i = 0;
        string ans;
        for(; i < n; i++)
        {
            if(path[i] == '/')
                continue;
            int pos = path.find("/", i);
            string now;
            if(pos == string::npos)
                now = path.substr(i);
            else
                now = path.substr(i, pos - i);

            if(now == ".")
                continue;
            else if(now == "..")
            {
                int j = ans.rfind("/");
                if(j != string::npos)
                {
                    ans.erase(j, ans.size());
                }
            }
            else
            {
                ans += "/" + now;
            }
            if(pos == string::npos)
                break;
            i = pos;
        }
        if(ans.size() == 0)
            ans += "/";
        return ans;
    }
};